<?php
/*
 * Author : nefeli
 * Improved By : basanas & pandiam
 */
session_start();

require_once ('include/css_head.php');
require_once ('include/functions.php');
require_once ('include/header.php');
require_once ('../BL/BL.php');
require_once ('../frameworks/objects/user.php');

if (!isset($_SESSION['uid']) || $_SESSION['uid'] == -1)
{
    UI_registerError('msg_warning', "You are not logged in.");
    header("Location:index.php");
}

$user = BL_getUserData($_SESSION['uid']);
?>

<div id="profileSideBar" style="min-width: 260px; margin-right: 50px; float:left"></div>

<div style="margin-left: 290px" id="content">
        <?php
        if (!isset($_GET['page']))
            printUserTable();
        else if (isset($_GET['page']) && ($_GET['page'] == "edit"))
            printUserForm();
        else if (isset($_GET['page']) && ($_GET['page'] == "categories"))
        {
            // Creator //
            echo '<h2>Categories where you have creator rights.</h2>';
            echo "<div class='block blockYellow'>";
            $arr = BL_get_categories_with_owner($_SESSION['uid']);
            if (!is_object($arr) && $arr < 0 )
                BL_registerError($arr);
            else
            {
                echo "\n    <div>\n";
                if ( empty($arr) )
                {
                    echo "        No such categories";
                }
                else
                {
                    echo "\n        <ul>\n";
                    foreach ( $arr as $cat )
                    {
                        //print_r($cat);
                        echo "\n<li style='margin-top:10px'>".$cat[1];
                        echo "<a style='margin-left:20px' href='editCategory.php?id=".$cat[0]."'>(<img class='smallImg' src='images/editCategory.png'/> Edit</a>  ";
                        echo "<a href='deleteCategory.php?id=".$cat[0]."'><img class='smallImg' src='images/deleteContent.png'/> Delete</a>  -  ";
                        echo "<a href='insertEntity.php?id=".$cat[0]."'>( <img class='smallImg' src='images/addEntity.png'/> Add entities</a>  -  ";
                        echo "<a href='selectEntity.php?id=".$cat[0]."'><img class='smallImg' src='images/editEntity.png'/> Edit entities</a>  -  ";
                        echo "<a href='deleteEntity.php?id=".$cat[0]."'><img class='smallImg' src='images/deleteContent.png'/> Delete entities</a> )";
                        echo "</li>\n";
                    }
                    echo "\n        </ul>\n";
                }
                echo "\n    </div>\n";
            }
            echo "</div>";

            // Moderator //
            echo '<h2 style="margin-top: 20px">Categories where you have moderator rights.</h2>';
            echo "<div class='block blockYellow'>";
            $arr = BL_get_categories_with_moderator($_SESSION['uid']);
            if (!is_object($arr) && $arr < 0 )
                BL_registerError($arr);
            else
            {
                echo "\n    <div>\n";
                if ( empty($arr) )
                {
                    echo "        No such categories";
                }
                else
                {
                    echo "\n        <ul>\n";
                    foreach ( $arr as $cat )
                    {
                        //print_r($cat);
                        echo "\n<li style='margin-top:10px'>".$cat[1];
                        echo "<a style='margin-left:20px' href='editCategory.php?id=".$cat[0]."'>(<img class='smallImg' src='images/editCategory.png'/> Edit</a> )  ";
                        echo "<a href='insertEntity.php?id=".$cat[0]."'>( <img class='smallImg' src='images/addEntity.png'/> Add entities</a>  -  ";
                        echo "<a href='selectEntity.php?id=".$cat[0]."'><img class='smallImg' src='images/editEntity.png'/> Edit entities</a>  -  ";
                        echo "<a href='deleteEntity.php?id=".$cat[0]."'><img class='smallImg' src='images/deleteContent.png'/> Delete entities</a> )";
                        echo "</li>\n";
                    }
                    echo "\n        </ul>\n";
                }
                echo "\n    </div>\n";
            }
            echo "</div>";

            // Member //
            echo '<h2 style="margin-top: 20px">Categories where you participate.</h2>';
            echo "<div class='block blockYellow'>";
            $arr = BL_get_categories_with_member($_SESSION['uid']);
            if (!is_object($arr) && $arr < 0 )
                BL_registerError($arr);
            else
            {
                echo "\n    <div>\n";
                if ( empty($arr) )
                {
                    echo "        No such categories";
                }
                else
                {
                    echo "\n        <ul>\n";
                    foreach ( $arr as $cat )
                    {
                        //print_r($cat);
                        echo "\n<li style='margin-top:10px'>".$cat[1];
                        echo "<a href='insertEntity.php?id=".$cat[0]."'>( <img class='smallImg' src='images/addEntity.png'/> Add entities</a>  -  ";
                        echo "<a href='selectEntity.php?id=".$cat[0]."'><img class='smallImg' src='images/editEntity.png'/> Edit entities</a>  -  ";
                        echo "<a href='deleteEntity.php?id=".$cat[0]."'><img class='smallImg' src='images/deleteContent.png'/> Delete entities</a> )";
                        echo "</li>\n";
                    }
                    echo "\n        </ul>\n";
                }
                echo "\n    </div>\n";
            }
            echo "</div>";

            // Flush errors //
            BL_flushErrors();
        }
        else if (isset($_GET['page']) && ($_GET['page'] == "content")) {
        ?>
            <p>Select the action you want to perform from the menu on the left.</p>
        <?php
        }
        else
            printUserTable();
        ?>
</div>

<script type="text/javascript">
        $.ajaxSetup ({ cache: false });
        var ajax_load = "<img src='images/load.gif' alt='loading...' />";
        $("#profileSideBar").load($("#profileSideBar").html(ajax_load).load('profileSideBar.php'));
</script>

<?php

require_once ('Include/footer.php');

function printUserTable()
{
    global $user;
    echo "<div id='editPersonalInfo'>";
    echo "<h2>Personal info</h2>";
    echo "<table class='blockGreen' style='width: 250px'>";
    echo "		<tr>";
    echo "			<td class='tC tCW' style='max-width:50px' >Username: </td>";
    echo "			<td class='tC tCC'>".$user->getUsername()."</td>";
    echo "		</tr>";
    echo "		<tr>";
    echo "			<td class='tC tCW'>Email: </td>";
    echo "			<td class='tCC tC'>".$user->getEmail()."</td>";
    echo "		</tr>";

    echo "		<tr>";
    echo "			<td class='tC tCW'>Name: </td>";
    echo "			<td class='tC tCC'>".$user->getName()."</td>";
    echo "		</tr>";
    echo "		<tr  >";
    echo "			<td class='tC tCW'>Surname: </td>";
    echo "			<td class='tC tCC'>".$user->getSurname()."</td>";
    echo "		</tr>";
    echo "</table>";

    echo "<form action='profile.php' method='GET'>";
    echo "<input type='hidden' name='page' value='edit' />";
    echo "<input style='height: 25px; margin-top: 20px;' type='submit' value='Edit'/>";
    echo "</form>";
    echo "</div>";
}

function printUserForm()
{
    global $user;
    echo "<div id='editPersonalInfo'>";
    echo "<h2>Personal info</h2>";
    echo "<table class='blockGrey' style='width: 350px'>";
    echo "<form action='editprofileDo.php' method='post'>";
    echo "		<tr align='right'>";
    echo "			<td class='tC tCW' style='max-width:50px'  >Username: </td>";
    echo "			<td class='tC tCC'>".$user->getUsername()."</td>";
    echo "<input type = 'hidden' name='username' value='".$user->getUsername()."' />";
    echo "		</tr>";
    echo "<tr align='right'>";
    echo "			<td class='tCW tC'>Old Password: </td>";
    echo "			<td class='tC tCC'> <input style='margin-top: 3px;' type='password' name='old_password' size='30' maxlength='30'  /> </td>";
    echo "		</tr>";
    echo "		<tr align='right'>";
    echo "			<td class='tCW tC'>New Password: </td>";
    echo "			<td class='tC tCC'> <input style='margin-top: 3px;' type='password' name='new_password' size='30' maxlength='30' /> </td>";
    echo "		</tr>";
    echo "		<tr align='right'>";
    echo "			<td class='tCW tC'>Confirm New Password: </td>";
    echo "			<td class='tC tCC'> <input style='margin-top: 3px;' type='password' name='new_password_check' size='30' maxlength='30' /> </td>";
    echo "		</tr>";
    echo "		<tr align='right'>";
    echo "			<td class='tCW tC'>Email: </td>";
    echo "			<td class='tC tCC'> <input style='margin-top: 3px;' type='text' name='email' size='30' maxlength='30' /> </td>";
    echo "		</tr>";
    echo "		<tr align='right'>";
    echo "			<td class='tCW tC'>Confirm Email: </td>";
    echo "			<td class='tC tCC'> <input style='margin-top: 3px;' type='text' name='email_check' size='30' maxlength='30' /> </td>";
    echo "		</tr>";
    echo "		<tr align='right'>";
    echo "			<td class='tCW tC'>Name:</td>";
    echo "			<td class='tC tCC'> <input style='margin-top: 3px;' type='text' name='name' size='30' maxlength='30' value='".$user->getName()."' /> </td>";
    echo "		</tr>";
    echo "		<tr align='right'>";
    echo "			<td class='tCW tC'>Surname:</td>";
    echo "			<td class='tC tCC'> <input style='margin-top: 3px;' type='text' name='surname' size='30' maxlength='30' value='".$user->getSurname()."' /> </td>";
    echo "		</tr>";
    echo "</table>";
    echo "<input style='height: 25px; margin: 20px 0 0 330px;' type='submit' value='Submit'/>";
    echo "</form>";
    echo "</div>";
}

?>
